package com.wx.wxapp.service;

import com.wx.wxapp.dao.*;
import com.wx.wxapp.entity.*;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

@Service
public class CardService {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /*
     *   功能：用户发布帖子
     *   参数：
     *       title：帖子标题
     *       information:帖子内容
     *       time:帖子时间
     *       label:帖子标签
     *       cardType:帖子类型
     *       openid:用户的openid
     * */
    @Transactional
    public String PublishCard(String title, String information, Date time, String label, String cardType, String openid) {
        if (null == title || null == information || null == time || null == label) {
            //抛出异常
            System.out.println("sd");
        }
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selectByOpenid(openid);
        int userId = user.getId();
        String userName = user.getNickname();
        String image = user.getImage();
        if (cardType.equals("food")) {
            String id = UUID.randomUUID().toString();
            Food food = new Food();
            Foodlabel label1;
            FoodMapper foodMapper = sqlSession.getMapper(FoodMapper.class);
            FoodlabelMapper foodlabelMapper = sqlSession.getMapper(FoodlabelMapper.class);
            String labels[] = label.split("&");
            for (String label2 : labels) {
                label1 = foodlabelMapper.selectByLabelName(label2);
                if(null != label1){
                    int num = label1.getNum();
                    num++;
                    label1.setNum(num);
                    foodlabelMapper.updateByPrimaryKey(label1);
                }else {
                    //没有该标签添加该标签
                    Foodlabel foodlabel = new Foodlabel();
                    foodlabel.setLabelname(label2);
                    foodlabel.setNum(1);
                    foodlabelMapper.insert(foodlabel);
                }
            }
            food.setId(id);food.setTitle(title);food.setLabel(label);food.setTime(time);food.setInformation(information);food.setUserid(userId);food.setUsername(userName);food.setPraisenumber(0);food.setComments(0);food.setImage(image);
            foodMapper.insert(food);
            sqlSession.close();
            return food.getId();
        } else {
            String id = UUID.randomUUID().toString();
            Disease disease = new Disease();
            Diseaselabel label1;
            DiseaseMapper diseaseMapper = sqlSession.getMapper(DiseaseMapper.class);
            DiseaselabelMapper diseaselabelMapper = sqlSession.getMapper(DiseaselabelMapper.class);
            String labels[] = label.split("&");
            for(int i =0;i<labels.length;i++){
                label1 = diseaselabelMapper.selectByLabelName(labels[i]);
                if(null != label1){
                    int num = label1.getNum();
                    num++;
                    label1.setNum(num);
                    diseaselabelMapper.updateByPrimaryKey(label1);
                }else {
                    Diseaselabel diseaselabel = new Diseaselabel();
                    diseaselabel.setLabelname(labels[i]);
                    diseaselabel.setNum(1);
                    diseaselabelMapper.insert(diseaselabel);
                }

            }
            disease.setId(id);disease.setTitle(title);disease.setLabel(label);disease.setTime(time);disease.setInformation(information);disease.setUseid(userId);disease.setUsername(userName);disease.setPraisenumber(0);disease.setComments(0);disease.setImage(image);
            diseaseMapper.insert(disease);
            sqlSession.close();
            return disease.getId();
        }
    }
}